package com.jsp.servlet; /**
 * @author wzm
 * @create 2023-03-13 10:17
 */

import com.jsp.entity.Employee;
import com.jsp.service.EmployeeService;
import com.jsp.service.impl.EmployeeServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/updateById")
public class UpdateByServlet extends HttpServlet {
    private EmployeeService service = new EmployeeServiceImpl();


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //收集表单中的数据
        Integer id = Integer.parseInt(request.getParameter("id"));
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Integer age =Integer.parseInt( request.getParameter("age"));
        String phone = request.getParameter("phone");
        Integer status =Integer.parseInt( request.getParameter("status"));

        //封装为对象
        Employee employee = new Employee(id,username,password,age,password,status);

        //调用Service方法
        boolean b = service.editEmp(employee);
        if(b){
            //修改成功
            response.sendRedirect(request.getContextPath()+"/getAll");
        }else{
            //失败
            response.getWriter().write("修改失败");
        }

    }
}
